const express = require('express');
const cors = require('cors');
const { getMapPoints } = require('./moduls/mongo');


const app = express();
const port = 3000;

app.use(cors()); // 允许跨域请求

// 模拟文章接口
const article = [
    {
        "id": 42,
        "title": "上传和更新项目到gitee",
        "content": "<h2>创建</h2><p><span style=\"color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); font-size: 16px;\">初始化 git init</span></p><p><span style=\"color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); font-size: 16px;\">添加仓库 git remote add origin 仓库.git</span></p><p><span style=\"color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); font-size: 16px;\">添加文件 git add .</span></p><p><span style=\"color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); font-size: 16px;\">上传 git push -u origin master</span></p><h2>更新</h2><p>添加文件 git add .</p><p>添加备注 git commit -m \"更新了某某功能\"</p><p><span style=\"color: rgb(51, 51, 51); background-color: rgb(255, 255, 255); font-size: 16px;\">上传 git push origin master</span></p>",
        "visitNum": 0,
        "thumbNum": 0,
        "favourNum": 0,
        "userName": "admin",
        "userId": 35,
        "createTime": "2025-02-20T16:39:53",
        "updateTime": "2025-02-20T16:39:53"
    },
    {
        "id": 41,
        "title": "程序运行没有结果",
        "content": "<p>不支持写法</p><pre><code >while(~scanf(\" %s\",a))</code></pre><p><br></p>",
        "visitNum": 0,
        "thumbNum": 0,
        "favourNum": 0,
        "userName": "admin",
        "userId": 35,
        "createTime": "2025-02-13T21:11:41",
        "updateTime": "2025-02-13T21:11:41"
    },
    {
        "id": 40,
        "title": "Docker向容器中的程序传递参数",
        "content": "<p>参考出处 &nbsp;<a href=\"https://www.yalexin.top/blog/blog/142\" target=\"_blank\">Docker-Java-Api操控Docker，并向容器中的程序传递参数（标准输入）</a> </p><p>用ApacheDockerHttpClient连接</p><pre><code class=\"language-java\">// 初始化连接\nURI uri = new URI(\"tcp://192.168.1.1:2375\");\nDockerHttpClient dockerHttpClient = new ApacheDockerHttpClient.Builder()\n    .dockerHost(uri)\n    .maxConnections(3000)\n    .build();\nDockerClient dockerClient = DockerClientBuilder.getInstance(config).withDockerHttpClient(dockerHttpClient).build();</code></pre><p>传递参数</p><pre><code >// 模拟输入\nString inputString = \"2 5\\n\";\n// 转为输入流\nbyte[] inputBytes = inputString.getBytes(StandardCharsets.UTF_8);\nInputStream inputStream = new ByteArrayInputStream(inputBytes);\n// 传入回调函数并执行\ntry {\n    dockerClient.execStartCmd(execCreateCmdResponse.getId())\n        .withStdIn(inputStream)\n        .exec(execStartResultCallback)\n        .awaitCompletion();\n} catch (InterruptedException e) {\n    e.printStackTrace();\n}</code></pre><p><br></p>",
        "visitNum": 0,
        "thumbNum": 0,
        "favourNum": 0,
        "userName": "admin",
        "userId": 35,
        "createTime": "2025-02-13T17:26:36",
        "updateTime": "2025-02-13T17:26:36"
    }
];

app.get('/', (req, res) => {
  res.json(article);
});

// ✅ 设置静态资源目录
app.use('/images', express.static('public/images'));


// 地图点数据接口
app.get('/api/points', async (req, res) => {
  try {
    const data = await getMapPoints();
    res.json(data);
  } catch (e) {
    res.status(500).send('获取地图数据失败: ' + e);
  }
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});
